package day01;

import java.util.*;

/**
 * @author DCSGO
 * @version 1.0
 */
public class P1101 {
    int n;
    ArrayList<Offset> offsets = new ArrayList<>();
    String word = "yizhong";
    Set<Point> showPoints = new HashSet<>();
    ArrayList<String> matrix = new ArrayList<>();
    ArrayList<Point> points = new ArrayList<>();
    Scanner scanner = new Scanner(System.in);

    /**
     * @param curPoint 矩阵中的点
     * @param direc    正在尝试的方向
     * @param curI     已经判断到word的第几个字母(0开始)
     */
    public void dfs(Point curPoint, int direc, int curI) {
        //已经判断到完毕
        if (curI == word.length()) {
            showPoints.addAll(points);
            points.clear();
            return;
        }

        if (curPoint.x < 0 || curPoint.x >= n || curPoint.y < 0 || curPoint.y >= n) {
            return;
        }

        //匹配第一个字母
        if (curI == 0) {
            //匹配 遍历8个方向
            if (matrix.get(curPoint.y).charAt(curPoint.x) == word.charAt(0)) {
                for (int d = 0; d < offsets.size(); d++) {
                    points.add(curPoint);
                    Offset off = offsets.get(d);
                    Point nextPoint = new Point(curPoint.x + off.x, curPoint.y + off.y);
                    dfs(nextPoint, d, curI + 1);
                    points.clear();
                }

            } else {
                points.clear();
                return;
            }
        }

        //当前字母匹配
        if (matrix.get(curPoint.y).charAt(curPoint.x) == word.charAt(curI)) {
            points.add(curPoint);
            Offset off = offsets.get(direc);
            Point nextPoint = new Point(curPoint.x + off.x, curPoint.y + off.y);
            dfs(nextPoint, direc, curI + 1);
        }
    }

    public static void main(String[] args) {
        /*
8
hgfhfhgf
ccddeeff
hghgshgf
gnohziyy
iiiiiiii
wbgfbgbg
hgfshtrt
nfgshgrt
         */
        P1101 question = new P1101();

        for (int y = 0; y < question.n; y++) {
            for (int x = 0; x < question.n; x++) {
                question.dfs(new Point(x, y), 0, 0);
            }
        }

        for (int y = 0; y < question.n; y++) {
            for (int x = 0; x < question.n; x++) {
                if (question.showPoints.contains(new Point(x, y))) {
                    System.out.print(question.matrix.get(y).charAt(x));
                } else {
                    System.out.print("*");
                }
            }
            System.out.println();
        }
    }

    public P1101() {
        offsets.add(new Offset(0, -1));//0 上
        offsets.add(new Offset(1, -1));//1 右上
        offsets.add(new Offset(1, 0));//2 右
        offsets.add(new Offset(1, 1));//3 右下
        offsets.add(new Offset(0, 1));//4 下
        offsets.add(new Offset(-1, 1));//5 左下
        offsets.add(new Offset(-1, 0));//6 左
        offsets.add(new Offset(-1, -1));//7 左上

        n = scanner.nextInt();
        for (int i = 0; i < n; i++) {
            matrix.add(scanner.next());
        }
    }
}

class Point {
    int x;
    int y;

    @Override
    public String toString() {
        return "(" + x + "," + y + ")";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Point point = (Point) o;
        return x == point.x && y == point.y;
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y);
    }

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

class Offset {
    int x;
    int y;

    @Override
    public String toString() {
        return "x=" + x + ", y=" + y;
    }

    public Offset(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
